/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.dao;

import wargame.entity.*;
import connectionmanager.DB;

/**
 *
 * @author Clarence
 */
public class TurnInformationDAO {

    public static TurnInformation retrieveTurnInformation(int turn_information_id) {
        String sql = "SELECT `turn_information_id`, `player_id`, `current_turn` FROM `turn_information` WHERE turn_information_id = '" + turn_information_id + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        TurnInformation ti = null;
        if (db.next()) {
            ti = new TurnInformation();
            ti.setTurn_information_id(db.getInt("turn_information_id"));
            ti.setPlayer_id(db.getInt("player_id"));
            ti.setCurrent_turn(db.getInt("current_turn"));
        }
        return ti;
    }

    public static int insertTurnInformation(TurnInformation ti) {
        String sql = "INSERT INTO turn_information (`player_id`, `current_turn`) VALUES ('" +
                ti.getPlayer_id() + "', '" + ti.getCurrent_turn() + "');";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        int lastInsertedId = db.getLastInsertedID();
        db.close();
        return lastInsertedId;
    }

    public static void updateTurnInformation(TurnInformation ti) {
        String sql = "UPDATE turn_information SET `player_id` = '" + ti.getPlayer_id() +
                "', `current_turn` = '" + ti.getCurrent_turn() +
                "' WHERE turn_information_id = '" + ti.getTurn_information_id() + "';";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static int getCurrentTurn() {
        String sql = "SELECT `current_turn` FROM `turn_information` WHERE player_id = 0 ORDER BY current_turn DESC";
        DB db = new DB();
        db.connect();
        db.query(sql);
        int current_turn = -1;
        if (db.next()) {
            current_turn = db.getInt("current_turn");
        }
        return current_turn;
    }
}
